library(dplyr)
# Keep only 2 columns
select(porgs, id, age)
# Drop the mass column
select(porgs, -mass)
# Put the age column first, but
# keep everything else the same
select(porgs, age, everything())
# Sort by age w/ YOUNGEST on top
arrange(porgs, age)
# Sort by age w/ ELDEST on top
arrange(porgs, desc(age))
# Sort by color and then by age
arrange(porgs, color, desc(age))
| Function | Order of Input | Output |
|---|---|---|
mdy() |
Month-Day-Year :: 05-18-2019 |
2019-05-18 |
mdy_hm() |
Month-Day-Year Hour:Minutes :: 05-18-2019 8:35 |
2019-05-18 08:35:00 UTC |
mdy_hms() |
Month-Day-Year Hour:Mins:Secs :: 05-18-2019 8:35:22 |
2019-05-18 08:35:22 UTC |
| Function | Date element |
|---|---|
year() |
Year |
month() |
Month as 1,2,3 |
day() |
Day of the month |
wday() |
Day of the week |
hour() |
Hour of the day (24hr) |
tz() |
Time zone |
left_join() keeps all rows and columns in the left table, but only keeps rows in the right table that match.
# Table w/ porg ages and heights
porgs
# Table w/ porg names
porg_names
# Join together by id columns
together <- left_join(porgs,
porg_names,
by = "id")
library(readr)
# Save data to a CSV text file
write_csv(porgs, "my_porg_data.csv")
library(ggsave)
# Save the last plot you viewed
ggsave("2019_porg_plot.png")
# Save an earlier named plot
ggsave(best_plot, "the_best_plot.png")
r or rstats + "question"[r] tagHelp > Cheatsheets? in the Console# Function help
?read_csv
# Search help
help.search("boxplot")
CTRL+ENTERCTRL+Shighlight+CTRL+Shift+A#rstats on
Use ifelse() to create new values that depend on the value of another column. For example, to only label the porgs with a height over 60cm as “tall”.
# If a porg's height is > 60cm label it as "tall",
# otherwise label as "short"
mutate(porgs, label = ifelse(height > 60, "tall", "short"))
Melinda.Ronca-Battista
Jaime.Yazzie
Vallen.Cook
Kristie.Ellickson
Dorian.Kvale